package com.jn.fastjson.views;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.jn.fastjson.R;

public class BookDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);

        String bookJsonString = getIntent().getStringExtra("book_json");
        JSONObject bookJson = JSON.parseObject(bookJsonString);
        JSONObject data = bookJson.getJSONObject("result").getJSONObject("data");

        String imgUrl = data.getString("img");
        Log.d("BookDetailActivity", "Image URL: " + imgUrl);

        ImageView bookImage = findViewById(R.id.book_image);
        TextView bookTitle = findViewById(R.id.book_title);
        TextView bookCatalog = findViewById(R.id.book_catalog);
        TextView bookTags = findViewById(R.id.book_tags);
        TextView bookSub1 = findViewById(R.id.book_sub1);
        TextView bookSub2 = findViewById(R.id.book_sub2);
        TextView bookReading = findViewById(R.id.book_reading);
        TextView bookOnline = findViewById(R.id.book_online);
        TextView bookBytime = findViewById(R.id.book_bytime);

        Glide.with(this)
                .load(imgUrl)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        Log.e("BookDetailActivity", "Glide load failed: " + e.getMessage());
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        Log.d("BookDetailActivity", "Glide load success");
                        return false;
                    }
                })
                .into(bookImage);

        bookTitle.setText(data.getString("title"));
        bookCatalog.setText("分类: " + data.getString("catalog"));
        bookTags.setText("标签: " + data.getString("tags"));
        bookSub1.setText(data.getString("sub1"));
        bookSub2.setText(data.getString("sub2"));
        bookReading.setText("阅读量: " + data.getString("reading"));
        bookOnline.setText("在线购买: " + data.getString("online"));
        bookBytime.setText("发布时间: " + data.getString("bytime"));
    }
}
